﻿using SqlSugar;
using System.Linq.Expressions;
using WebApi.Core.IRepository;
using WebApi.Core.IRepository.Base;
using WebApi.Core.IService;
using WebApi.Core.Model;
using WebApi.Core.Model.ResponseModels;
using WebApi.Core.Repository;
using WebApi.Core.Service.Base;
using WebApi.Core.Common;

namespace WebApi.Core.Service
{
    public class UserService:BaseService<Users>,IUserService
    {
        private readonly IUserRepository _userRepository;
        // 为什么需要 : base(userRepo)
        //1. 继承机制要求
        //子类构造函数必须初始化父类 
        //基类BaseService已定义构造函数需要IBaseRepository<T> 
        //必须通过: base(...)传递参数完成父类初始化

        //IUserRepository传递给基类BaseService的构造函数。这是因为基类BaseService需要IBaseRepository<TEntity> 来执行通用的CRUD操作。
        //由于IUserRepository继承自IBaseRepository<Users>，因此可以将子类的仓储实例传递给基类的构造函数，确保基类能够访问通用的仓储方法。
        public UserService(IUserRepository userRepository) : base(userRepository)
        {
            _userRepository = userRepository;
        }
        
        public async Task<PageResult<Users>> QueryUsersAsync(string? queryUser, bool? isActive, int pageIndex, int pageSize)
        {
            Expression<Func<Users,bool>> where = x => true;

            if (!string.IsNullOrEmpty(queryUser))
                where = where.And(x => x.UserId.Contains(queryUser) || x.UserName.Contains(queryUser));

            if (isActive.HasValue) 
                where = where.And(x => x.IsActive == isActive); 

            return await base.QueryPageAsync(where, pageIndex, pageSize ,x => x.UserId);
        }



    }
}
